using UnityEngine;

public class HitMarks : MonoBehaviour
{
	public static HitMarks instance;

	public MarkAnimation centralMark;

	public MarkAnimation[] marks;

	public MarkAnimation[] arrowMarks;

	public int MinAlpha_0_100;

	private float _minAlpha;

	private float _maxAlpha;

	public int MaxAlpha_0_100;

	public float DurationSec;

	public int maxDamage;

	private Vector3 myPositions;

	private int startDamage;

	private Vector3 pos;

	private void Awake()
	{
		instance = this;
		MaxAlpha_0_100 = Mathf.Abs(MaxAlpha_0_100);
		MinAlpha_0_100 = Mathf.Abs(MinAlpha_0_100);
		if (MinAlpha_0_100 >= 100)
		{
			MinAlpha_0_100 = 0;
		}
		if (MaxAlpha_0_100 > 100)
		{
			MaxAlpha_0_100 = 100;
		}
		if (MinAlpha_0_100 > MaxAlpha_0_100)
		{
			MinAlpha_0_100 = 0;
		}
		if (DurationSec < 0.5f)
		{
			DurationSec = 0.5f;
		}
		_minAlpha = (float)MinAlpha_0_100 / 100f;
		_maxAlpha = (float)MaxAlpha_0_100 / 100f;
		myPositions = Vector3.zero;
	}

	private void OnEnable()
	{
		ResetMark();
		if (centralMark.widget != null)
		{
			centralMark.widget.alpha = 0f;
		}
	}

	public void ResetMark()
	{
		for (int i = 0; i < marks.Length; i++)
		{
			if (marks[i].widget != null)
			{
				marks[i].widget.alpha = 0f;
			}
		}
		for (int j = 0; j < arrowMarks.Length; j++)
		{
			if (arrowMarks[j].widget != null)
			{
				arrowMarks[j].widget.alpha = 0f;
			}
		}
	}

	public float? RotateDamage(int enemyID)
	{
		if (GameController.instance.myPlayer == null || enemyID == 0 || enemyID == -99999)
		{
			return null;
		}
		Vector3 eulerAngles = GameController.instance.tpsCamera.gameObject.transform.eulerAngles;
		Vector3 position = GameController.instance.myPlayer.gameObject.transform.position;
		string text = CameraModeControl.instance.mainCameraType.ToString();
		PhotonView photonView = PhotonView.Find(enemyID);
		if (photonView == null)
		{
			return null;
		}
		float? result = null;
		Vector3 position2 = photonView.gameObject.transform.position;
		Vector3 rhs = position2 - position;
		Vector3 vector = position - GameController.instance.tpsCamera.gameObject.transform.position;
		Vector3 up = GameController.instance.myPlayer.gameObject.transform.up;
		Vector3 forward = GameController.instance.myPlayer.gameObject.transform.forward;
		switch (text)
		{
		case "FPSCamera":
			result = Mathf.Atan2(Vector3.Dot(up, Vector3.Cross(forward, rhs)), Vector3.Dot(forward, rhs)) * 57.29578f;
			break;
		case "RPGCamera":
		case "TPSCamera":
			result = Mathf.Atan2(rhs.x * vector.z - vector.x * rhs.z, rhs.x * vector.x + vector.z * rhs.z) * 57.29578f;
			break;
		}
		return result;
	}

	public float? RotateDamage(Vector3 enemyPosition)
	{
		string text = CameraModeControl.instance.mainCameraType.ToString();
		if (GameController.instance == null || GameController.instance.myPlayer == null)
		{
			return 0f;
		}
		myPositions = GameController.instance.myPlayer.gameObject.transform.position;
		Vector3 up = GameController.instance.myPlayer.gameObject.transform.up;
		Vector3 forward = GameController.instance.myPlayer.gameObject.transform.forward;
		Vector3 rhs = enemyPosition - myPositions;
		Vector3 vector = myPositions - GameController.instance.tpsCamera.gameObject.transform.position;
		float? result = null;
		switch (text)
		{
		case "FPSCamera":
			result = Mathf.Atan2(Vector3.Dot(up, Vector3.Cross(forward, rhs)), Vector3.Dot(forward, rhs)) * 57.29578f;
			break;
		case "RPGCamera":
		case "TPSCamera":
			result = Mathf.Atan2(rhs.x * vector.z - vector.x * rhs.z, rhs.x * vector.x + vector.z * rhs.z) * 57.29578f;
			break;
		}
		return result;
	}

	public void ShowHit(float? iAngle, int damage)
	{
		if (iAngle.HasValue)
		{
			while (iAngle.HasValue && iAngle.Value < 0f)
			{
				float? num = iAngle;
				iAngle = ((!num.HasValue) ? null : new float?(num.Value + 360f));
			}
			while (iAngle.HasValue && iAngle.Value >= 360f)
			{
				float? num2 = iAngle;
				iAngle = ((!num2.HasValue) ? null : new float?(num2.Value - 360f));
			}
			int num3 = Mathf.RoundToInt(iAngle.Value / 45f);
			if (num3 == 8)
			{
				num3 = 0;
			}
			centralMark.transform.eulerAngles = new Vector3(0f, 0f, 180f - iAngle.Value);
			float num4 = damage / maxDamage;
			centralMark.widget.alpha = CurrentSize(num4 + centralMark.widget.alpha);
			marks[num3].widget.alpha = CurrentSize(num4 + marks[num3].widget.alpha);
			centralMark.Init(DurationSec);
			marks[num3].Init(DurationSec);
		}
	}

	public void ShowStartHit(float? iAngle, int damage, Vector3 enemyPos)
	{
		if (iAngle.HasValue)
		{
			while (iAngle.HasValue && iAngle.Value < 0f)
			{
				float? num = iAngle;
				iAngle = ((!num.HasValue) ? null : new float?(num.Value + 360f));
			}
			while (iAngle.HasValue && iAngle.Value >= 360f)
			{
				float? num2 = iAngle;
				iAngle = ((!num2.HasValue) ? null : new float?(num2.Value - 360f));
			}
			startDamage = damage;
			if (enemyPos != Vector3.zero)
			{
				pos = enemyPos;
			}
			else
			{
				pos = GameController.instance.myPlayer.gameObject.transform.position;
			}
			ShowHits();
		}
	}

	public void ShowHits()
	{
		float num = startDamage / maxDamage;
		MarkAnimation[] array = arrowMarks;
		foreach (MarkAnimation markAnimation in array)
		{
			if (!markAnimation.isFree)
			{
				markAnimation.isFree = true;
				markAnimation.widget.alpha = CurrentSize(num + markAnimation.widget.alpha);
				markAnimation.position = pos;
				markAnimation.InitArrow(DurationSec);
				break;
			}
		}
	}

	private float CurrentSize(float alpha)
	{
		if (alpha < _minAlpha)
		{
			alpha = _minAlpha;
		}
		else if (alpha > _maxAlpha)
		{
			alpha = _maxAlpha;
		}
		return alpha;
	}
}
